package com.imooc.spark.streaming.windowOperations

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
  * 当在（K，V）对的DStream上调用时，返回（K，Long）对的新DStream，
  * 其中每个键的值是其在滑动窗口内的频率。
  * 同样 reduceByKeyAndWindow，reduce任务的数量可通过可选参数进行配置。
  *
  * @description countByValueAndWindow（windowLength， slideInterval，[ numTasks ]）
  * @author yuyon26@126.com
  * @date 2018/10/24 20:15
  */
object CountByValueAndWindow {
  def main(args: Array[String]): Unit = {

    val sparkConf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount")
    val ssc = new StreamingContext(sparkConf, Seconds(5))
    ssc.checkpoint("D:\\spark\\checkpoint")
    ssc.sparkContext.setLogLevel("ERROR")

    val lines = ssc.socketTextStream("192.168.31.30", 9999)

    val fmapLines = lines.flatMap(_.split(" "))
    val countByValueAndWindow = fmapLines.countByValueAndWindow(Seconds(10), Seconds(5))

    countByValueAndWindow.print()

    ssc.start()
    ssc.awaitTermination()
  }
}
